home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib_tools / RDFPipe.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.2 KB  |  95 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from pprint import pprint
  5. from rdflib.Namespace import Namespace
  6. from rdflib import plugin, RDF, RDFS, URIRef
  7. from rdflib.store import Store
  8. from rdflib.Graph import Graph
  9. from rdflib.syntax.NamespaceManager import NamespaceManager
  10. RDFLIB_CONNECTION = ''
  11. RDFLIB_STORE = 'IOMemory'
  12. import getopt
  13. import sys
  14.  
  15. def usage():
  16.     print "USAGE: RDFPipe.py [options]\n    \n    Options:\n    \n      --stdin                     Parse RDF from STDIN (useful for piping)\n      --help                      \n      --input-format              Format of the input document(s).  One of:\n                                  'xml','trix','n3','nt','rdfa'\n      --output                    Format of the final serialized RDF graph.  One of:\n                                  'n3','xml','pretty-xml','turtle',or 'nt'\n      --ns=prefix=namespaceUri    Register a namespace binding (QName prefix to a \n                                  base URI).  This can be used more than once"
  17.  
  18.  
  19. def main():
  20.     
  21.     try:
  22.         (opts, args) = getopt.getopt(sys.argv[1:], '', [
  23.             'output=',
  24.             'ns=',
  25.             'input=',
  26.             'stdin',
  27.             'help',
  28.             'input-format='])
  29.     except getopt.GetoptError:
  30.         e = None
  31.         print e
  32.         usage()
  33.         sys.exit(2)
  34.  
  35.     factGraphs = []
  36.     factFormat = 'xml'
  37.     useRuleFacts = False
  38.     nsBinds = {
  39.         'rdf': RDF.RDFNS,
  40.         'rdfs': RDFS.RDFSNS,
  41.         'owl': 'http://www.w3.org/2002/07/owl#',
  42.         'dc': 'http://purl.org/dc/elements/1.1/',
  43.         'foaf': 'http://xmlns.com/foaf/0.1/',
  44.         'wot': 'http://xmlns.com/wot/0.1/' }
  45.     outMode = 'n3'
  46.     stdIn = False
  47.     if not opts:
  48.         usage()
  49.         sys.exit()
  50.     
  51.     for o, a in opts:
  52.         if o == '--input-format':
  53.             factFormat = a
  54.             continue
  55.         if o == '--stdin':
  56.             stdIn = True
  57.             continue
  58.         if o == '--output':
  59.             outMode = a
  60.             continue
  61.         if o == '--ns':
  62.             (pref, nsUri) = a.split('=')
  63.             nsBinds[pref] = nsUri
  64.             continue
  65.         if o == '--input':
  66.             factGraphs = a.split(',')
  67.             continue
  68.         if o == '--help':
  69.             usage()
  70.             sys.exit()
  71.             continue
  72.     
  73.     store = plugin.get(RDFLIB_STORE, Store)()
  74.     store.open(RDFLIB_CONNECTION)
  75.     namespace_manager = NamespaceManager(Graph())
  76.     for prefix, uri in nsBinds.items():
  77.         namespace_manager.bind(prefix, uri, override = False)
  78.     
  79.     factGraph = Graph(store)
  80.     factGraph.namespace_manager = namespace_manager
  81.     if factGraphs:
  82.         for fileN in factGraphs:
  83.             factGraph.parse(fileN, format = factFormat)
  84.         
  85.     
  86.     if stdIn:
  87.         factGraph.parse(sys.stdin, format = factFormat)
  88.     
  89.     print factGraph.serialize(destination = None, format = outMode, base = None)
  90.     store.rollback()
  91.  
  92. if __name__ == '__main__':
  93.     main()
  94.  
  95.